package com.goods.controller.business;


import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/supplier")
public class supplierController {

    @Autowired
    private SupplierService SupplierService;

    /**
     * 查询所有物资来源
     * @param pageNum 起始页
     * @param pageSize 每页显示条数
     * @param name  查询内容
     * @return
     */
    @GetMapping("/findSupplierList")
    public ResponseBean findSupplierList(@RequestParam("pageNum") int pageNum,
                                         @RequestParam("pageSize") int pageSize,
                                         @RequestParam("name") String name){
        try {
            //调用后台查询数据
            PageVO<SupplierVO> pageVO = SupplierService.findSupplierList(pageNum,pageSize,name);
            if (pageVO != null){
                return ResponseBean.success(pageVO);
            }
            return ResponseBean.error("没有关于"+name+"的信息");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询物资来源失败");
        }
    }

    /**
     * 添加物资来源
     * @param supplier
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Supplier supplier){
        try {
            SupplierService.add(supplier);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("添加失败");
        }
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        SupplierVO supplierVO = SupplierService.edit(id);
        if (supplierVO != null){
            return ResponseBean.success(supplierVO);
        }else {
            return ResponseBean.error("数据回显失败");
        }
    }

    /**
     * 提交编辑信息
     * @param
     * @return
     */
    @PutMapping("/update/{id}")
    public ResponseBean update(@RequestBody Supplier supplier){
        try {
            SupplierService.update(supplier);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("修改失败");
        }
    }

    /**
     * 删除物资来源信息
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        try {
            SupplierService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("删除失败");
        }
    }

    /**
     * 查询所有以知来源
     * @return
     */
    @GetMapping("/findAll")
    public ResponseBean findAll(){
        try {
            List<SupplierVO> list = SupplierService.findAll();
            return ResponseBean.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询所有已知来源失败");
        }
    }

}
